﻿<script>
    common.setMainTitle('Store Management'+ '<div>&nbsp;</div>');
</script>

<form id="mainform" name="frm" method="POST" action="/admin/store/index">
	<div id="button-toolbar">
		<div class="item" onclick="add()" id="addButton" title="Add">
			<div id="addnew-global-button"></div>
		</div>
		
		<div class="item" onclick="deleteAll()" id="deleteButton" title="Delete">
			<div id="delete-global-button"></div>
		</div>
		
		<div class="item" onclick="changeStatusMultiItems(1)" id="activateButton" title="Activate">
			<div id="save-global-button"></div>
		</div>
		
		<div class="item" onclick="changeStatusMultiItems(0)" id="deactivateButton" title="Deactivate">
			<div id="back-global-button"></div>
		</div>
		
		<div class="item" onclick="changeFeaturedMultiItems(1)" id="featuredButton" title="Featured">
			<div id="featured-global-button"></div>
		</div>
		
		<div class="item" onclick="changeFeaturedMultiItems(0)" id="nonfeaturedButton" title="Non-featured">
			<div id="non-featured-global-button"></div>
		</div>
	</div>
    
	<table class="store-search" >	
        <tr>
            <td width="10%" nowrap="nowrap">
					<strong>Store name</strong>
					<input type=text class="inp-form" id="storename" name="storename" value="<?=$this->storename?>">
            </td>
			<td width="10%" nowrap="nowrap">
					<strong>Email</strong>
					<input type=text class="inp-form" id="email" name="email" value="<?=$this->email?>" >
            </td>
			<td width="10%" nowrap="nowrap">
					<strong>Category</strong>
					<select type=text id="storecategoryid" name="storecategoryid" value="<?=$this->storecategoryid?>">
						<option value="">All</option>
						<?php 
								foreach($this->listStoreCategory as $item){
									if($item['PARENTID']==0){
										$level = 0;
										$spaceLevel = "__";
										echo '<option  value="'.$item['STORECATEGORYID'].'" ';
										if($item['STORECATEGORYID'] == $this->storecategoryid)
											echo 'selected';
										else 
											echo '';
										echo '><b>'.$item['NAME'].'</b></option>';
									}
									else {
										echo '<option value="'.$item['STORECATEGORYID'].'" ';
										if($item['STORECATEGORYID'] == $this->storecategoryid)
											echo 'selected';
										else 
											echo '';
										echo '><b>|____'.$item['NAME'].'</b></option>';
									}
								}
						?>
					</select>
            </td>
			<td width="10%" nowrap="nowrap" >
					<strong>Status</strong>
					<select type=text id="status"  name="status" value="<?=$this->status?>">	
						<option style="margin: 5px" value=""  <?= ($this->status=="")? 'selected':"" ?>  >All</option>
						<option value="1" <?= ($this->status=="1")? 'selected':"" ?> >Published</option>
						<option value="0" <?= ($this->status=="0")? 'selected':"" ?> >Unpublished</option>
					</select>
            </td>
            <td>
				<input type='submit' style="padding: 5px" value="Search">
            </td>
        </tr>
    </table >
	
	<div class="first-char-filter">		
		<div class="item"><a href='javascript:filterFirstCharacter("A","a")' > A </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("B","b")' > B </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("C","c")' > C </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("D","d")' > D </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("E","e")' > E </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("F","f")' > F </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("G","g")' > G </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("H","h")' > H </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("I","i")' > J </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("K","k")' > K </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("L","l")' > L </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("M","m")' > M </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("N","n")' > N </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("O","o")' > O </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("P","p")' > P </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("Q","q")' > Q </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("R","r")' > R </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("S","s")' > S </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("T","t")' > T </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("U","u")' > U </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("V","V")' > V </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("W","w")' > W </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("X","x")' > X </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("Y","y")' > Y </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("Z","z")' > Z </a></div>
		<div class="item">|</div>
		<div class="item"><a href='javascript:filterFirstCharacter("","")' > ALL </a></div>
		<input type="hidden" name="HIGHER" id="HIGHER" value="<?= $this->higher?>"/>
		<input type="hidden" name="LOWER" id="LOWER" value="<?= $this->lower?>"/>
	</div>
	
    <table style="margin-top: 10px; clear: both;" border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
        <thead>
            <tr>
                <th width="3%" class="table-header-repeat line-left" style="text-align: center">
                    <input type="checkbox" name="DELALL" id="ALLSELECTOR" onclick="selectAll(this,'ITEM')"/>
                </th>
                <th class="table-header-repeat line-left" style="text-align: left" width="20%"><a>NAME</a></th>
                <th class="table-header-repeat line-left" style="text-align: left" width="20%"><a>ADDRESS</a></th>
                <th class="table-header-repeat line-left" style="text-align: left" width="10%"><a>EMAIL</a></th>
                <th class="table-header-repeat line-left" style="text-align: left" width="12%"><a>CREATED DATE</a></th>
                <th class="table-header-repeat line-left" style="text-align: left" width="12%"><a>LAST UPDATE</a></th>
				<th class="table-header-repeat line-left" style="text-align: left" width="7%"><a>FEATURED</a></th>
                <th class="table-header-repeat line-left" style="text-align: left" width="7%"><a>STATUS</a></th>
                <th class="table-header-repeat line-left" style="text-align: left" width="12%"><a>ACTION</a></th>
            </tr>
        </thead>
        <tbody>
            <?php
                if(count($this->data) == 0){
                    echo "<tr><td colspan='9'> There is not any data</td></tr>";
                }else{
                    foreach($this->data as $item){
                        ?>
                        <tr style="cursor: pointer">
                            <td style="text-align: center; padding-left: 0px;">
                                <input id="<?=$item['STOREID']?>" type="checkbox" name="ITEM[]" value="<?= $item['STOREID']?>"/>
                            </td>
                            <td>
                                <div>
                                    <?=$item['NAME'];?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['ADDRESS'].", ".$item['WARD'].", ".$item['DISTRICT'].", ".$item['CITY'] ?>    
                                </div>
                            </td>
                            
                            <td>
                                <div id="EMAIL<?=$item['STOREID']?>">
                                    <?=$item['EMAIL']?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['CREATEDDATE']?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['LASTUPDATE']?>    
                                </div>
                            </td>
                            
                            <td>
                                <span id="feature<?=$item['STOREID']?>">
									<? if($item['ISFEATURED']!='1') { ?>
										<a href="#" onClick="changeFeatured(<?=$item['STOREID'] ?>,1)">Featured</a>
									<?} else {?>
										<a href="#" onClick="changeFeatured(<?=$item['STOREID'] ?>,0)">Non</a>
									<?}?>
								</span>
                            </td>
                            
                            <td>
                                <span id="status<?=$item['STOREID']?>">
									<? if($item['STATUS']=='1') { ?>
										<a href="#" onClick="changeStatus(<?=$item['STOREID'] ?>,0)">Published</a>
									<?} else {?>
										<a href="#" onClick="changeStatus(<?=$item['STOREID'] ?>,1)">Unpublished</a>
									<?}?>
								</span>
                            </td>
                            
                            <td>
                                <div>
								<select id="<?php echo 'actionbox'.$item['STOREID'] ?>" name="<?php echo 'actionbox'.$item['STOREID'] ?>" onchange="chooseAction(<?=$item['STOREID'] ?>)">
									<option value="0" >Choose an Action</option>
									<option value="<?=$item['STOREID'] ?>" >Edit</option>
									<option value="<?=$item['STOREID'] ?>" >Delete</option>
									<option value="<?=$item['STOREID'] ?>" >Change Owner</option>
								</select>
								</div>
                            </td>
                        </tr>
                        <?php
                    }
                } 
                
            ?>
        </tbody>
    </table>
    <div id="pagination-div" width="100%">
        <?= $this->pagination?>
    </div>
	<div id="change-owner-dialog" title="Change Store Owner"> 
	
</div>
</form>

<script type="text/javascript">
    
	function chooseAction(storeid){
		text = $("#actionbox"+storeid+ " option:selected").text();
		if(text == 'Edit'){
			/*window.location = "/admin/store/input/id/"+storeid + "/limit/" + '<?= $this->limit?>/page/' + '<?= $this->page?>';
			*/
			window.location = "/admin/store/input/id/"+storeid;
		}else if(text == "Delete"){
			check = confirm("Do you want to delete this store?");
			if(check){
				window.location = "/admin/store/delete/id/" +storeid + "/limit/" + '<?= $this->limit?>/page/' + '<?= $this->page?>';
			}
		}else if(text == "Change Owner"){
			$('#actionbox' + storeid + " option[value ='0']").attr('selected','');
			$('#change-owner-dialog').dialog({
				modal: true,
				width: 475,
				height: 180,
				resizable: false,
				buttons : {
					"Save": function(){ save();}
				}
				});
				$.ajax({
				url: '/admin/store/changeowner',
				data: {itemID : storeid}, 
				type: 'POST',
				success: function(data) {
					if(data){
						$('#change-owner-dialog').html(data);
					}
				}
			});
		}
    }
	
	function selectAll(selobj,childobj){
        var arr = document.getElementsByName(childobj+"[]");
         for(var i=0;i<arr.length;i++){
            arr[i].checked = selobj.checked;
         }
    }
    
    function add(){
        window.location = '/admin/store/input';
    }
	
    /*  Thay đổi trạng thái status */
    function changeStatus(itemID, status){
        $.ajax({
			url: "/admin/store/changestatus",
			type: "POST",
			data: {itemID : itemID, status: status},
			dataType: "text",
			success: function(data) {
				if(status == 1){
					$('#status' + itemID).html('<a href="#" onClick="changeStatus('+ itemID+ ',0)">Published</a>');
				} else {
					$('#status' + itemID).html('<a href="#" onClick="changeStatus('+ itemID+ ',1)">Unpublished</a>');
				}
			}
		});
    }
	
	/* Thay đổi trạng thái của status */
	function changeStatusMultiItems(status){
		var ln = 0;
		var arr = document.getElementsByName('ITEM[]');
		var arrValue = new Array();
			
		for(var i = 0 ; i < arr.length ;i++ )
		{
				if(arr[i].checked == true)
				{
						arrValue[i] = arr[i].value;
				}
		}
		for(var i = 0 ; i < arr.length ;i++ )
		{

				if(arr[i].checked == true)
				{
						ln = 1;
						break;
				}
		}
		if(ln == 1)
		{
		    var question;
		    if(status ==1){
			    question = 'Do you want to publish these stores?';
		    } else {
			    question = 'Do you want to unpublish these stores?';
		    }
		    
		    if(confirm(question)){
				$.ajax({
					url: "/admin/store/changestatusmultiitems",
					type: "POST",
					data: {itemIDs : arrValue, status: status},
					dataType: "text",
					success: function(data) {
						for(var i = 0 ; i < arr.length ;i++ )
						{
							if(status == 0){
								$('#status' + arrValue[i]).html('<a href="#" onClick="changeStatus('+ arrValue[i]+ ',1)">Published</a>');
							} else {
								$('#status' + arrValue[i]).html('<a href="#" onClick="changeStatus('+ arrValue[i]+ ',0)">Unpublished</a>');
							}
							arr[i].checked=false;
						}
					}
				});
		    }
			$('#ALLSELECTOR').attr('checked', false);
			
		}
		else
		{
		    alert("You must choose at least 1 store!");
		}
	}
	
	/*  Thay đổi store thành featured hay non-featured */
    function changeFeatured(itemID, featured){
        $.ajax({
			url: "/admin/store/changefeatured",
			type: "POST",
			data: {itemID : itemID, featured: featured},
			dataType: "text",
			success: function(data) {
				if(featured == 0){
					$('#feature' + itemID).html('<a href="#" onClick="changeFeatured('+ itemID+ ',1)">Featured</a>');
				} else {
					$('#feature' + itemID).html('<a href="#" onClick="changeFeatured('+ itemID+ ',0)">Non</a>');
				}
			}
		});
    }
	
	/* Thay đổi trạng thái featured của nhiều store */
	function changeFeaturedMultiItems(featured){
		var ln = 0;
		var arr = document.getElementsByName('ITEM[]');
		var arrValue = new Array();
			
		for(var i = 0 ; i < arr.length ;i++ )
		{
				if(arr[i].checked == true)
				{
						arrValue[i] = arr[i].value;
				}
		}
		for(var i = 0 ; i < arr.length ;i++ )
		{

				if(arr[i].checked == true)
				{
						ln = 1;
						break;
				}
		}
		if(ln == 1)
		{
		    var question;
		    if(status ==1){
			    question = 'Do you want to set these stores are featured?';
		    } else {
			    question = 'Do you want to set these stores are non-featured?';
		    }
		    
		    if(confirm(question)){
				$.ajax({
					url: "/admin/store/changefeaturedmultiitems",
					type: "POST",
					data: {itemIDs : arrValue, featured: featured},
					dataType: "text",
					success: function(data) {
						for(var i = 0 ; i < arr.length ;i++ )
						{
							if(featured == 1){
								$('#feature' + arrValue[i]).html('<a href="#" onClick="changeFeatured('+ arrValue[i]+ ',1)">Featured</a>');
							} else {
								$('#feature' + arrValue[i]).html('<a href="#" onClick="changeFeatured('+ arrValue[i]+ ',0)">Non</a>');
							}
							arr[i].checked=false;
						}
					}
				});
		    }
			$('#ALLSELECTOR').attr('checked', false);
			
		}
		else
		{
		    alert("You must choose at least 1 store!");
		}
	}
	
    function deleteAll()
    {
		var ln = 0;
		var arr = document.getElementsByName('ITEM[]');
		for(var i = 0 ; i < arr.length ;i++ )
		{

			if(arr[i].checked == true)
			{
			ln = 1;
			break;
			}
		}
		if(ln == 1)
		{
			if(confirm("<?php echo "Do you want to delete these stores?"; ?>")){
			document.frm.action="/admin/store/deletemultiitem/limit/" + '<?= $this->limit?>/page/' + '<?= $this->page?>';
			document.frm.method="post";
			document.frm.submit();
			}
		}
		else
		{
			alert("You must choose at least 1 store!");
		}
    }
	
	function filterFirstCharacter(higher,lower){
		$('#HIGHER').attr('value',higher);
		$('#LOWER').attr('value',lower);
		$('#mainform').submit();
    }
	
</script>